package morenoapp.components.thinkgraphv2.components;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import morenoapp.MorenoApp;
import morenoapp.components.thinkgraphv2.components.CheckBoxListTable.DataItem;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.model.listeners.MorenoModelAdapter;
import morenoapp.model.view2.MorenoView2Data;
import morenoapp.model.view2.MorenoView2Face;
import morenoapp.util.I18N;

/**
 *
 * @author dmitry.mamonov
 */
@Deprecated
public class RelationshipsDisplayConfigJPanel extends javax.swing.JPanel {
    final MorenoView2Face config;
    /** Creates new form RelationshipsDisplayConfigJPanel */
    public RelationshipsDisplayConfigJPanel(MorenoView2Face _config) {
        super();
        this.config = _config;

        initComponents();
        initComponentsCustom();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelShowRelationships = new javax.swing.JLabel();
        jScrollPane = new javax.swing.JScrollPane();
        sliderAnswerLevelFilter = new javax.swing.JSlider();
        labelAnswersLevelFilter = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(RelationshipsDisplayConfigJPanel.class);
        jLabelShowRelationships.setText(resourceMap.getString("jLabelShowRelationships.text")); // NOI18N
        jLabelShowRelationships.setName("jLabelShowRelationships"); // NOI18N

        jScrollPane.setName("jScrollPane"); // NOI18N

        sliderAnswerLevelFilter.setMajorTickSpacing(1);
        sliderAnswerLevelFilter.setMaximum(6);
        sliderAnswerLevelFilter.setMinimum(1);
        sliderAnswerLevelFilter.setPaintTicks(true);
        sliderAnswerLevelFilter.setSnapToTicks(true);
        sliderAnswerLevelFilter.setName("sliderAnswerLevelFilter"); // NOI18N

        labelAnswersLevelFilter.setText(resourceMap.getString("labelAnswersLevelFilter.text")); // NOI18N
        labelAnswersLevelFilter.setName("labelAnswersLevelFilter"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabelShowRelationships, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
            .addComponent(jScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(sliderAnswerLevelFilter, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelAnswersLevelFilter, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelShowRelationships)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelAnswersLevelFilter)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sliderAnswerLevelFilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelShowRelationships;
    private javax.swing.JScrollPane jScrollPane;
    private javax.swing.JLabel labelAnswersLevelFilter;
    private javax.swing.JSlider sliderAnswerLevelFilter;
    // End of variables declaration//GEN-END:variables
    private void initComponentsCustom() {
        // questions selector
        final CheckBoxListTable selectQuestionsTable = new CheckBoxListTable(new CheckBoxListTable.Handler<MorenoQuestionFace>() {
            @Override
            public List<DataItem<MorenoQuestionFace>> provideData() {
                List<DataItem<MorenoQuestionFace>> result = new ArrayList();
                for (MorenoQuestionFace quest : MorenoApp.getModel().getQuestionsList()) {
                    result.add(new DataItem<MorenoQuestionFace>(quest, quest.getNumber() + "." + quest.getContent(), config.isDisplayRelationships(quest)));
                }
                return result;
            }

            @Override
            public void applyChanges(DataItem<MorenoQuestionFace> aItem) {
                config.setDisplayRelationships(aItem.getId(), aItem.isSelected());
            }

        });
        jScrollPane.setViewportView(selectQuestionsTable);

        //add listeners
        MorenoApp.getModel().addModelListener(new MorenoModelAdapter() {
            @Override
            public void onQuestionChanged(MorenoQuestionFace question) {
                selectQuestionsTable.refreshData();
            }

            @Override
            public void onQuestionsListChanged() {
                selectQuestionsTable.refreshData();
            }

        });



        { //count of displayer relationships
            sliderAnswerLevelFilter.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (config.getDisplayFirstNRelationships()!=sliderAnswerLevelFilter.getValue()){
                        config.setDisplayFirstNRelationships(sliderAnswerLevelFilter.getValue());
                    }
                }
            });
            config.addPropertyChangeListener(new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (MorenoView2Data.PROPERTY_DISPLAYFIRSTNRELATIONSHIPS.equals(evt.getPropertyName())){
                        int newValue = (Integer)evt.getNewValue();
                        if (sliderAnswerLevelFilter.getValue()!=newValue){
                            sliderAnswerLevelFilter.setValue(newValue);
                        }
                        labelAnswersLevelFilter.setText(I18N.get("sociogram.relationship.level." + config.getDisplayFirstNRelationships()));
                    }
                }
            });
        }
    }
}
